function detail(param) {
    this.el = $(param.el);
    this.baseUrl = ' http://app.trtpre.com/v1.0.0/';
    this.url = ' http://app.trtpre.com/v1.0.0/goods/goods/';
    
    this.init();
}
detail.prototype = {
    init: function() {
        this.userId = this.getId().userId;
        this.getData();
        this.bindBom();
        if (this.getQueryString('share')) {
            $('header').hide();
            $('.footer').show();
            $('.content-wrapper').addClass('margin-bottom')
        }
    },
    bindBom: function() {
      var _this = this;
        // $(document).on('click', '.btn-back', function() {
        //     history.back(-1);
        // })
        $(document).on('click', '.btn-x', function() {
          var $this = $(this);
          if($this.hasClass('on')){
            _this.delFavorite();
          }else{
            _this.favorite();
          }
          
        })
        $(document).on('click', '.btn-f', function() {
          try{
            window.showShareMenu.callNative()              
          }catch(e){
            console.log(e)
          }
          
        })
    },
    html: function(res) {

        var str = '<div class="title">\
      <img src="' + res.resourceUrl + '">\
      <div class="title-bg"></div>\
    </div>\
    <div class="content">\
      <h2>' + res.name + '</h2>\
      <h3>' + res.hashtag + '</h3>\
      <p class="description">' + res.memo + '</p>\
      <ul class="item-tit">\
        <li><span><em>性味</em>' + res.dodder + '</span></li>\
        <li><span><em>归经</em>' + res.meridian + '</span></li>\
        <li><span><em>适宜体质</em>' + res.suitContitution + '</span></li>\
      </ul>\
      <div class="tit_head"><span class="tit_head__tips">食材功效</span></div>\
      <ul class="item-gx">';
        for (var i = 0; i < res.effects.length; i++) {
            var item = res.effects[i];
            str += '<li>\
                  <h4>' + item.name + '</h4>\
                  <p>' + item.memo + '</p>\
                </li>'
        }
        str += '</ul>\
      <div class="tit_head"><span class="tit_head__tips">食用禁忌</span></div>\
      <ul class="item-jj">';
        for (var i = 0; i < res.taboo.length; i++) {
            var item = res.taboo[i];
            str += '<li>\
                  <p>' + item + '</p>\
                </li>'
        }
        str += '</ul>\
    </div>';
        return str;
    },
    getData: function() {
        var _this = this;
        var uid = this.getId().uid
        var goodsId = this.getQueryString('goodsId');
        var data = {
          userId:this.getId().userId
        }
        $.get(this.url+goodsId, data)
            .then(function(res) {
                if (res.msg == 'OK') {
                    _this.id = res.result.id;
                    _this.reResourceUrl = res.result.resourceUrl;
                    _this.identifierName = res.result.name;
                    _this.el.html(_this.html(res.result))
                    if (res.result.favorite) {
                        $('.btn-x').addClass('on')
                    }
                }
            })
    },
    favorite: function() {
        var _this = this;
        var uid = this.getId().uid
        var data = {
          daCategoryId:25,
          identifier:_this.id,
          identifierName:_this.identifierName,
          reResourceUrl:_this.reResourceUrl
        }
        $.post(this.baseUrl+'user/users/'+this.getId().userId+'/favorites',data)
            .then(function(res) {
                if (res.msg == 'OK') {
                    $('.btn-x').addClass('on')
                }
            })
    },
    delFavorite:function(){
        var _this = this;
        var uid = this.getId().uid
        var data = {
          daCategoryId:25,
          identifier:_this.id
        }
        $.ajax({
          type: 'delete',
          url:_this.baseUrl+'user/users/'+_this.getId().userId+'/favorites?daCategoryId=25&identifier='+_this.id,
          data:{} ,
          success:function(res){
            if (res.msg == 'OK') {
                 $('.btn-x').removeClass('on')
              }
          }
        });
    },
    getQueryString: function(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    },
    getId: function() {
        var newObject = {
            userId:'',
            uid: '',
            tb: ''
        };
        var ua = navigator.userAgent;
        var newArr = ua.split(';');
        for (var i = 0; i < newArr.length; i++) {
            if (newArr[i].indexOf('userId') != -1) {
                newObject.userId = newArr[i].replace('userId:', '');
                continue;
            }
            if (newArr[i].indexOf('uid') != -1) {
                newObject.uid = newArr[i].replace('uid:', '')
                continue;
            }
            if (newArr[i].indexOf('tb') != -1) {
                newObject.tb = newArr[i].replace('tb:', '')
                continue;
            }
        }
        return newObject;
    }
}
